﻿/*
 * Copyright (c) 2011 Shawn Rakowski
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to 
 * deal in the Software without restriction, including without limitation the 
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 
 * sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 * SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using ROS.Persistence;

namespace ROS.Application.DomainObjects
{
    internal class User : DomainObject
    {
        #region Fields

        private long id;
        private String firstName;
        private String lastName;
        private bool isActive;
        private UserType userType;
        IList<OrderEvent> orderEvents;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the ID of this User.
        /// </summary>       
        public long ID
        {
            get { return id; }
            set { id = value; }
        }

        /// <summary>
        /// Gets or sets whether or not this user is active.
        /// </summary>
        public bool IsActive
        {
            get { return isActive; }
            set { isActive = value; }
        }

        /// <summary>
        /// Gets or sets the first name of this user.
        /// </summary>        
        public String FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        /// <summary>
        /// Gets or sets the last name of this user.
        /// </summary>       
        public String LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        /// <summary>
        /// Gets or sets the type of user this is.
        /// </summary>        
        public UserType UserType
        {
            get { return userType; }
            set { userType = value; }
        }

        /// <summary>
        /// Get OrderEvents this User has been involved in.
        /// </summary>
        /// <remarks>
        /// I am breaking encapulation of Persistence to implement lazy loading here. Later
        /// this should be refactored to be gathered via a virtual proxy.
        /// </remarks>
        public IList<OrderEvent> OrderEvents
        {
            get 
            {
                if (orderEvents == null)
                {
                    OrderEventDataMapper orderEventDM = new OrderEventDataMapper();
                    orderEvents = orderEventDM.FindOrderEventsByUser(this);
                }
                return orderEvents;
            }
        }

        /// <summary>
        /// Gets whether or not this User may be deleted.
        /// </summary>
        public bool MayBeDeleted 
        {
            get
            {
                return OrderEvents.Count == 0;
            }
        }

        /// <summary>
        /// Gets whether or not this user is a manager.
        /// </summary>
        public bool IsManager
        {
            get { return userType == Application.DomainObjects.UserType.MANAGER; }
            set
            {
                bool setToManager = value;
                if (this.IsManager && setToManager ||
                    !this.IsManager && !setToManager)
                    return;

                if (!setToManager)
                {
                    this.MakeStaff();
                }
                else
                {
                    this.MakeManager();
                }
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Construct a new User object.
        /// </summary>
        public User(String firstName, String lastName, UserType userType)
        {
            this.id = 0;
            this.isActive = true;
            this.firstName = firstName;
            this.lastName = lastName;            
            this.userType = userType;
        }

        /// <summary>
        /// Construct a new User object.
        /// </summary>
        public User(long id, bool isActive, String firstName, String lastName, Application.DomainObjects.UserType userType)
        {
            this.id = id;
            this.isActive = isActive;
            this.firstName = firstName;
            this.lastName = lastName;
            this.userType = userType;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Checks whether or not this User may cancel a fulfilled order.
        /// </summary>
        /// <returns>true if they may cancel a fulfilled order.</returns>
        internal bool MayCancelFulfilledOrder()
        {
            return userType == Application.DomainObjects.UserType.MANAGER;
        }

        /// <summary>
        /// Deactivates this User object.
        /// </summary>
        internal void Deactivate()
        {
            isActive = false;
        }

        /// <summary>
        /// Make this User a staff member but not a manager.
        /// </summary>
        private void MakeStaff()
        {
            userType = Application.DomainObjects.UserType.STAFF;
        }

        /// <summary>
        /// Make this user a manager.
        /// </summary>
        private void MakeManager()
        {
            userType = Application.DomainObjects.UserType.MANAGER;
        }

        #endregion        
    }
}
